package bpm.facade.document;

import bpm.domain.document.Attachment;
import bpm.domain.document.AttachmentOriginType;
import bpm.domain.document.AttachmentType;
import bpm.domain.document.AttachmentView;
import bpm.domain.document.Document;
import bpm.domain.document.AttachmentType.AttachmentTypeKey;
import bpm.domain.user.OrgUser;

/**
 * 
 * @author ZhaoWangLong Aug 16, 2011
 * 
 */
public class AttachmentTransition {
	public static Attachment transAttachmentView(AttachmentView attachView) {
		Attachment attachment = new Attachment();
		attachment.setId(attachView.getId());
		Integer caseId=attachView.getCaseId();
		Integer workItemId=attachView.getWorkItemId();
		if(caseId>0&&workItemId>0)
			attachment.setScope(caseId+"/"+workItemId);
		AttachmentType type = new AttachmentType();
		type.setId(attachView.getAttachmentTypeId());
		type.setName(attachView.getAttachmentTypeName());
		AttachmentTypeKey typeKey = AttachmentTypeKey.valueOf(attachView
				.getAttachmentTypeKey());
		type.setKey(typeKey);
		attachment.setType(type);
		attachment.setReferencedResourceType(AttachmentOriginType
				.valueOf(attachView.getReferencedResourceType()));
		attachment
				.setReferencedResourceId(attachView.getReferencedResourceId());
		Document doc = new Document();
		doc.setId(attachView.getDocumentId());
		attachment.setOwnedDocument(doc);
		attachment.setPaper(attachView.isPaper());
		attachment.setTitle(attachView.getTitle());
		attachment.setSize(attachView.getSize());
		OrgUser creator = new OrgUser();
		creator.setId(attachView.getCreator());
		creator.setName(attachView.getCreatorName());
		attachment.setCreator(creator);
		OrgUser lastUpdater = new OrgUser();
		lastUpdater.setId(attachView.getLastUpdater());
		lastUpdater.setName(attachView.getLastUpdaterName());
		attachment.setLastUpdater(lastUpdater);
		attachment.setCreateTime(attachView.getCreateTime());
		attachment.setLastUpdateTime(attachView.getLastUpdateTime());
		attachment.setDescription(attachView.getDescription());
		return attachment;
	}

	public static AttachmentView transAttachment(Attachment attachment) {
		if (attachment != null) {
			AttachmentView attachView = new AttachmentView();
			attachView.setId(attachment.getId());
			String scope=attachment.getScope();
			if(scope!=null&&!"".equals(scope)){
				String[] process=scope.split("/");
				if(process.length>1){
					attachView.setCaseId(Integer.parseInt(process[0]));
					attachView.setWorkItemId(Integer.parseInt(process[1]));
				}else{
					attachView.setCaseId(Integer.parseInt(process[0]));
				}
			}
			attachView.setAttachmentTypeId(attachment.getType() == null ? null
					: attachment.getType().getId());
			attachView
					.setAttachmentTypeName(attachment.getType() == null ? null
							: attachment.getType().getName());
			attachView.setAttachmentTypeKey(attachment.getType() == null ? null
					: (attachment.getType().getKey() == null ? null
							: attachment.getType().getKey().name()));
			attachView.setReferencedResourceType(attachment
					.getReferencedResourceType().name());
			attachView.setReferencedResourceId(attachment
					.getReferencedResourceId());
			attachView
					.setDocumentId(attachment.getOwnedDocument() == null ? null
							: attachment.getOwnedDocument().getId());
			attachView.setPaper(attachment.isPaper());
			attachView.setTitle(attachment.getTitle());
			attachView.setSize(attachment.getSize());
			attachView.setCreator(attachment.getCreator() == null ? null
					: attachment.getCreator().getId());
			attachView.setCreatorName(attachment.getCreator() == null ? null
					: attachment.getCreator().getName());
			attachView
					.setLastUpdater(attachment.getLastUpdater() == null ? null
							: attachment.getLastUpdater().getId());
			attachView
					.setLastUpdaterName(attachment.getLastUpdater() == null ? null
							: attachment.getLastUpdater().getName());
			attachView.setCreateTime(attachment.getCreateTime());
			attachView.setLastUpdateTime(attachment.getLastUpdateTime());
			attachView.setDescription(attachment.getDescription());
			return attachView;
		} else {
			return null;
		}
	}

}
